package sapling.web.admin;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sapling.dao.AdminDao;
import sapling.form.AdminForm;
import sapling.model.Company;
import sapling.service.AdminService;
import sapling.service.CompanyService;

import java.util.Objects;

/**
 * Created by ever on 2016/10/9.
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminAction {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin")
    public String list(Model model) {
        model.addAttribute("admin",adminDao.findByUsernameNot("admin"));
        return "admin/admin/admin_list";
    }

    @RequestMapping("/create")
    public String create() {
        return "admin/admin/admin_create";
    }

    @RequestMapping("/save")
    public String save(AdminForm form) {
       if(form.getUsername().length()>0){
           adminService.save(form);
       }
       return "redirect:/admin/admin";
    }

    @RequestMapping("/delete")
    public String delete(Long admin_id){
        if(Objects.nonNull(admin_id)){
            adminDao.delete(admin_id);
        }
        return "redirect:/admin/admin";
    }

    @RequestMapping("/password/save")
    public String passwordSave(AdminForm form) {
        adminService.passwordSave(form);
        SecurityUtils.getSubject().logout();
        return "redirect:/login";
    }
}
